package com.runfast.waimai.web.controller.shopper;

import com.runfast.paotui.dao.model.Role;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.paotui.utils.JiGuangPushUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * ${DESCRIPTION} 骑手
 *
 * @author mengxiang
 * @create 2018-05-29  12:27   /server/shopper/beginwork
 **/
@Controller
@RequestMapping("/server" +
        "/shopper/*")
public class DriverMsgController {
    @Resource
    RunfastShopperService shopperService;
    @PostMapping("add") //添加骑手信息，中增加了全职 身份证号
    public @ResponseBody ModelMap add(@RequestBody RunfastShopper shopper,
                                      @RequestParam(value = "password",required = false,defaultValue = "123456")String password
    ){

        System.out.println(shopper+"“*****************");


        ModelMap model=new ModelMap();
        if(shopper!=null){
               RunfastShopper a= shopperService.findByNumeber(shopper.getMunber());
               if(a !=null){
                   model.put("msg","手机号已被 注册过");
                   model.put("success",false);
                   return  model;
               }
                   RunfastShopper b= shopperService.findByidnumber(shopper.getIdNumber());
                   if(b!=null){
                       model.put("msg","身份证已被 注册过");
                       model.put("success",false);
                       return  model;
                   }
                       shopper.setPassword(DigestUtils.md5Hex(password));
                       shopper.setCreateTime(new Date());
                       shopperService.insertSelective(shopper);
                       model.put("success",true);



        } else {
            model.put("msg","添加失败");
            model.put("success",false);
        }


        return  model;
    }
    @PostMapping("update1") //更新骑手的状态
    public @ResponseBody ModelMap update1(@RequestBody RunfastShopper shopper){
        ModelMap model=new ModelMap();
        System.out.println(shopper.getEquipment()+"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"+shopper.getId());
        if(shopper!=null){
            shopperService.updateByPrimaryKeySelective(shopper);

            model.put("success",true);
            model.put("msg","更新成功");
        }else {
            model.put("success",false);
            model.put("msg","更新失败");
        }
        return model;
    }
    @PostMapping("delete1") //骑手的状态
    public @ResponseBody ModelMap delete1(@RequestBody RunfastShopper shopper){
        ModelMap model=new ModelMap();
        if(shopper.getId()!=null){
            shopperService.deteleshopper(shopper.getId());
            model.put("success",true);
            model.put("msg","成功删除");
        }else {
            model.put("success",false);
            model.put("msg","失败");

        }
        return model;
    }
    //  @PostMapping("findmax") //多项条件 查询骑手 上班查询 ，代理查询，全职兼职查询， 姓名模糊查询（姓名 可以是一个字，但不能为空，其它查询条件可以为空）
    @GetMapping("findmax")
    public @ResponseBody ModelMap findmax(
            Integer working, Integer worktype, Integer agentid ,String name,@RequestParam(value="page",required=false,defaultValue = "0")Integer page,@RequestParam(value="pagesize",required=false,defaultValue = "10")Integer pagesize,Integer status,String equipment
    ,Integer type){
        ModelMap model=new ModelMap();
        Integer page1=page*pagesize;


        String name1="%"+name+"%";

        List<RunfastShopper> runfastShopperList=shopperService.findagentidAndWordtypeAndWorkingAndName(agentid,worktype,working,name1,page1,pagesize,status,equipment,type);
    //    List<RunfastShopper> runfastShopperList1=shopperService.findagentidAndWordtypeAndWorkingAndName(agentid,worktype,working,name1,page1,pagesize,status,equipment);

        Integer count2=shopperService.findagentidAndWordtypeAndWorkingAndNamecount1(agentid,worktype,working,name1,page1,pagesize,status,equipment);
        Integer count3=shopperService.findagentidAndWordtypeAndWorkingAndNamecount2(agentid,worktype,working,name1,page1,pagesize,status,equipment);
        Integer count4=shopperService.findagentidAndWordtypeAndWorkingAndNamecount3(agentid,worktype,working,name1,page1,pagesize,status,equipment);
        Integer count5=shopperService.findagentidAndWordtypeAndWorkingAndNamecount4(agentid,worktype,working,name1,page1,pagesize,status,equipment);

        for(RunfastShopper a: runfastShopperList){

            System.out.println(a);

        }
        Integer count=shopperService.findagentidAndWordtypeAndWorkingAndNamecount(agentid,worktype,working,name1,page1,pagesize,status,equipment);
        if(type==1){
            count=count2;
        }else if(type==2){
            count=count3;
        }else if(type==3){
            count=count4;
        }else if(type==4){
             count=count5;}
        else {

        }

        if(null==runfastShopperList){
            model.put("msg","没有你输入的条件的结果 ");
            model.put("success",false);

        }else {
            model.put("work1",count2);
            model.put("work0",count3);

          model.put("status1",count4);
           model.put("equipment",count5);
        model.put("list",runfastShopperList);
        model.put("count",count);
        model.put("success",true);}
        return model;
    }
    /*开工提醒  /server/shopper/beginwork
    * */
    @PostMapping("/beginwork")
    public @ResponseBody ModelMap beginwork(@RequestBody RunfastShopper runfastShopper1){
      //  System.out.println(token);
        RunfastShopper runfastShopper=shopperService.get(runfastShopper1.getId());
        System.out.println(runfastShopper+"######################");
        ModelMap model=new ModelMap();

        HashMap<String, String> params = new HashMap<>();
        List<String>aliases=new ArrayList<>();
        String url = "www.baidu.com";
        String title=runfastShopper.getName()+"大神！上班时间到了";

        if(runfastShopper.getAlias()!=null){
            params.put("name", runfastShopper.getName());
            aliases.add(runfastShopper.getAlias());
            JiGuangPushUtil.push(Role.Predefine.DRIVER, 10, aliases,title,params,null);
            model.put("success",true);
        }else {
            model.put("success",false);
            model.put("msg","你家的alias走丢了");
        }




        return model;
    }

}
